﻿/*
 * 
 * Authors: Martin Paulík, Michal Zbraněk
 * 
 */ 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PB138___projekt
{
    /// <summary>
    /// Form for insert category. 
    /// </summary>
    public partial class insertCategory : Form
    {
        XML xml = new XML();
        public insertCategory()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Stops showing of errorProvider.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void textBox1_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(textBox1, "");
        }
        /// <summary>
        /// Checking if textBox is valid. If text is empty, it shows errorProvider.
        /// Otherwise if the name exist, then it shows errorProvider. Otherwise insert category to xml file. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">CancelEventArgs</param>
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                e.Cancel = true;
                textBox1.Select(0, textBox1.Text.Length);
                errorProvider1.SetError(textBox1, "The name is required !!");
            }
            else
            {
                if (xml.getCategory(textBox1.Text) != null)
                {
                    e.Cancel = true;
                    textBox1.Select(0, textBox1.Text.Length);
                    errorProvider1.SetError(textBox1, "The name already exists, you use the other");
                }
                else
                {
                    if (xml.insertCategory(textBox1.Text, null) == true)
                        MessageBox.Show("Insert category done successfully", "SUCCESS",System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Information);
                    else
                        MessageBox.Show("Error, inserting have failed", "ERROR", System.Windows.Forms.MessageBoxButtons.OK,
                       System.Windows.Forms.MessageBoxIcon.Error);
                    textBox1.Clear();
                }
            }
        }
        /// <summary>
        /// If user click button Insert, calls validating of textBox. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1_Validating(new object(), new CancelEventArgs());
        }

    }
}
